package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.domain.ChannelData;
import cn.broadsense.biz.mapper.ChannelDataMapper;
import cn.broadsense.biz.service.ChannelDataService;
import cn.broadsense.biz.service.DataItemChannelService;
import cn.broadsense.biz.service.DataItemService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lijian
 * @description
 * @date 2024年09月23日 下午4:47
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ChannelDataServiceImpl extends ServiceImpl<ChannelDataMapper, ChannelData> implements ChannelDataService {

    private final DataItemService dataItemService;

    private final DataItemChannelService dataItemChannelService;

    @Override
    public void syncData(List<Long> ids) {
        log.info("===同步采集数据开始===");
        List<ChannelData> channelDataList = this.lambdaQuery().in(ChannelData::getId,ids).list();
        syncCollectChannelData(channelDataList);
        log.info("===同步采集数据结束===");
    }

    @Override
    public void syncDataForJob() {
        List<ChannelData> channelDataList = this.lambdaQuery().list();
        syncCollectChannelData(channelDataList);
    }


    public void syncCollectChannelData(List<ChannelData> channelDataList) {
        if (CollectionUtil.isNotEmpty(channelDataList)) {
           // List<Long> channelIds = channelDataList.stream().map(ChannelData::getChannelId).collect(Collectors.toList());
           //TODO  更新状态 同步到点位表 ？需求不确定
            this.update(Wrappers.lambdaUpdate(ChannelData.class)
                    .set(ChannelData::getSyncTime,new Date())
                    .in(ChannelData::getId, channelDataList.stream().map(ChannelData::getId).collect(Collectors.toList()))
            );
            /*  List<DataItemChannel> dataItemChannelList = dataItemChannelService.lambdaQuery()
                    .select(DataItemChannel::getId, DataItemChannel::getChannelId, DataItemChannel::getSyncTime)
                    .in(DataItemChannel::getChannelId, channelIds)
                    .eq(DataItemChannel::getIsSync, true)
                    .eq(DataItemChannel::getIsCollect, true)
                    .list();
            List<DataItem> dataItemList = dataItemService.lambdaQuery()
                    .select(DataItem::getId, DataItem::getChannelId, DataItem::getCollectData)
                    .in(DataItem::getChannelId, channelIds)
                    .list();
            channelDataList.forEach(x -> {
                Optional<DataItemChannel> first1 = dataItemChannelList.stream().filter(z -> z.getChannelId().equals(x.getChannelId())).findFirst();
                if (!first1.isPresent()) {
                    return;
                }
                ChannelData data = new ChannelData();
                data.setSyncTime(first1.get().getSyncTime());
                Optional<DataItem> first = dataItemList.stream().filter(y -> y.getChannelId().equals(x.getChannelId())).findFirst();
                if (first.isPresent()) {
                    DataItem dataItem = first.get();
                    data.setId(x.getId());
                    if (StrUtil.isNotBlank(dataItem.getCollectData())) {
                        JSONObject jsonObject = JSON.parseObject(dataItem.getCollectData());
                        data.setDeviceName(jsonObject.containsKey("deviceName") ? jsonObject.getString("deviceName") : x.getDeviceName());
                        data.setChannelCode(jsonObject.containsKey("channelCode") ? jsonObject.getString("channelCode") : x.getChannelCode());
                        data.setDeptName(jsonObject.containsKey("deptName") ? jsonObject.getString("deptName") : x.getDeptName());
                        data.setDeviceGps(jsonObject.containsKey("deviceGps") ? jsonObject.getString("deviceGps") : x.getDeviceGps());
                        data.setInstallAddress(jsonObject.containsKey("installLocation") ? jsonObject.getString("installLocation") : x.getInstallAddress());
                        data.setMonitorDirection(jsonObject.containsKey("monitorDirection") ? jsonObject.getString("monitorDirection") : x.getMonitorDirection());
                        data.setMonitorViewingArea(jsonObject.containsKey("monitorViewingArea") ? jsonObject.getString("monitorViewingArea") : x.getMonitorViewingArea());
                        data.setMonitorDistance(jsonObject.containsKey("monitorDistance") ? jsonObject.getBigDecimal("monitorDistance") : x.getMonitorDistance());
                        data.setDeviceMonitorHigh(jsonObject.containsKey("deviceMonitorHigh") ? jsonObject.getBigDecimal("deviceMonitorHigh") : x.getDeviceMonitorHigh());
                        data.setHighAndLowPoint(jsonObject.containsKey("highAndLowPoint") ? jsonObject.getString("highAndLowPoint") : x.getHighAndLowPoint());
                    }
                    this.updateById(data);
                }

                 });
                */

        }
    }
}
